<?php

namespace App\Http\Controllers\Agent\User;

use Illuminate\Http\Request;
use Session,Config;
use App\Http\Requests;
use App\Http\Service\User\UserService;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     *代理登录
     */
    public function login()
    {
        return view('Agent.User.login');
    }

    public function do_login(Request $request)
    {

        $msg=array();//定义返回信息数组
        $where = array();

        //   获取输入信息
        $mobile = $request->input('mobile');
        $where['password'] = $request->input('password');

        $userService = new UserService();

        if(preg_match("/^1[34578]{1}\d{9}$/",$mobile)){
            $where['mobile'] = $mobile;
        }else{
            return $data['content'] = '手机号码不正确！';
        }
        $data = $userService->login($where);
//dd($data);

        if($data['error_code']==0){
            //用户保存在Session
            $detial_data = $data['data'];

            Session::put(Config::get('custom.setting.MEMBER_TOKEN'),$detial_data['token']);
            Session::put(Config::get('custom.setting.MEMBER_CITY'),$detial_data['user_info']['city']);
            Session::put(Config::get('custom.setting.MEMBER_REGION'),$detial_data['user_info']['region']);
            Session::put(Config::get('custom.setting.MEMBER_UTYPE'),$detial_data['utype']);
            Session::put(Config::get('custom.setting.MEMBER_INFO'),$detial_data['user_info']);
            Session::put(Config::get('custom.setting.MEMBER_CODE'),$detial_data['user_info']['reg_code']);
            return redirect('/index');
        }else{
            // return $data;
            echo '<script>javascript:history.back();alert("'.$data['error_message'].'！");</script>';
        }



        dd($where);
    }


}
